package pers.xds.wtuapp.web.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import pers.xds.wtuapp.web.database.bean.Acquisition;
import pers.xds.wtuapp.web.database.mapper.AcquisitionMapper;
import pers.xds.wtuapp.web.database.mapper.UserMapper;
import pers.xds.wtuapp.web.database.view.AcquisitionQuery;
import pers.xds.wtuapp.web.es.bean.EsAcquisition;
import pers.xds.wtuapp.web.es.repository.EsAcquisitionRepository;
import pers.xds.wtuapp.web.service.AcquisitionService;

import java.util.List;
import java.util.NoSuchElementException;

/**
 * @author DeSen Xu
 * @date 2023-02-16 17:21
 */
@Service
public class AcquisitionServiceImpl implements AcquisitionService {

    private AcquisitionMapper acquisitionMapper;

    private EsAcquisitionRepository esAcquisitionRepository;

    private UserMapper userMapper;

    @Autowired
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Autowired
    public void setEsAcquisitionRepository(EsAcquisitionRepository esAcquisitionRepository) {
        this.esAcquisitionRepository = esAcquisitionRepository;
    }

    @Autowired
    public void setAcquisitionMapper(AcquisitionMapper acquisitionMapper) {
        this.acquisitionMapper = acquisitionMapper;
    }

    @Override
    public int postAcquisition(Acquisition acquisition) {
        acquisitionMapper.insert(acquisition);
        EsAcquisition esAcquisition = new EsAcquisition();
        esAcquisition
                .setId(acquisition.getId())
                .setTitle(acquisition.getTitle())
                .setCreateTime(System.currentTimeMillis())
                .setExpectPrice(acquisition.getExpectPrice())
                .setNickname(userMapper.selectNicknameOnly(acquisition.getOwnerId()))
                .setOwnerId(acquisition.getOwnerId());

        esAcquisitionRepository.save(esAcquisition);
        return acquisition.getId();
    }

    @Override
    public void takeDown(int acquisitionId, int ownerId) {
        if (acquisitionMapper.takeDownAcquisition(acquisitionId, ownerId) == 0) {
            throw new NoSuchElementException("订单不存在");
        }
    }

    @Override
    public List<AcquisitionQuery> queryList(Integer maxId, int size) {
        final int maxAllowSize = 10;
        size = Math.min(size, maxAllowSize);
        return acquisitionMapper.queryAcquisition(maxId, size);
    }

    @Nullable
    @Override
    public Acquisition queryById(int acquisitionId) {
        return acquisitionMapper.selectById(acquisitionId);
    }

    @Override
    public List<Acquisition> queryByActive(int uid, boolean isActive, int page, int size) {
        return acquisitionMapper.selectByActive(uid, isActive ? 1 : 0, new Page<>(page, size, false)).getRecords();
    }

    @Override
    public List<EsAcquisition> searchByTitle(String title, int page, int size) {
        final int maxSize = 10;
        size = Math.min(size, maxSize);
        // es的搜索从0开始
        page--;
        return esAcquisitionRepository.searchByTitle(title, PageRequest.of(page, size)).getContent();
    }

}
